package m1.server;

import m1.server.clearancerequest.ClearanceRequest;
import m1.server.connectionmanager.ConnectionManager;
import m1.server.database.Database;
import m1.server.securityquery.SecurityQuery;
import m2.component.iface.ComponentInterfacePortFourni;
import m2.configuration.Configuration;
import m2.configuration.iface.ConfigurationInterface;
import m2.connector.iface.RoleFrom;
import m2.connector.iface.RoleTo;
import m2.link.Attachment;
import m2.link.AttachmentFrom;
import m2.link.AttachmentTo;
import m2.link.Binding;
import m1.server.securitymanager.SecurityManager;
import m1.server.sqlquery.SQLQuery;

public class ServerConfiguration extends Configuration{

	private SecurityQuery securityQuery;
	private ClearanceRequest clearanceRequest;
	private ConnectionManager connectionManager;
	private SecurityManager securityManager;
	private Database database;
	private SQLQuery sqlQuery;
	private ReceiveRequest receiveRequest;
	
	public ServerConfiguration(String name) {
		super(name);
		
		//Components
		connectionManager = new ConnectionManager("ConnectionManager");
		securityManager = new SecurityManager("SecurityManager");
		database = new Database("Database");
		
		addComponent(connectionManager);
		addComponent(securityManager);
		addComponent(database);
		
		//Connectors
		securityQuery = new SecurityQuery("SecurityQuery");
		clearanceRequest = new ClearanceRequest("ClearanceRequest");
		sqlQuery = new SQLQuery("SQLQuery");
		
		addConnector(securityQuery);
		addConnector(clearanceRequest);
		addConnector(sqlQuery);
		
		receiveRequest = new ReceiveRequest("ReceiveRequest");
		addInterfaceFourni(new ConfigurationInterface("InterfaceReceiveRequest"), receiveRequest);
		
		Attachment connectionManagerToSqlQuery = new AttachmentTo("ConnectionManagerToSqlQuery", 
				connectionManager.getInterface("InterfaceDbQuery").getInterfaceRequis("DbQuery"), 
				(RoleTo) sqlQuery.getRole("Caller"));
		
		addAttachmentTo((AttachmentTo) connectionManagerToSqlQuery,
				connectionManager.getInterface("InterfaceDbQuery").getInterfaceRequis("DbQuery"), 
				(RoleTo) sqlQuery.getRole("Caller"));
		
		Attachment connectionManagerToClearanceRequest = new AttachmentTo("ConnectionManagerToClearanceRequest", 
				connectionManager.getInterface("InterfaceSecurityCheck").getInterfaceRequis("SecurityCheck"), 
				(RoleTo) clearanceRequest.getRole("Requestor"));
		
		addAttachmentTo((AttachmentTo) connectionManagerToClearanceRequest,
				connectionManager.getInterface("InterfaceSecurityCheck").getInterfaceRequis("SecurityCheck"), 
				(RoleTo) clearanceRequest.getRole("Requestor"));
		
		Attachment securityManagerToClearanceRequest = new AttachmentFrom("SecurityManagerToClearanceRequest", 
				securityManager.getInterface("InterfaceSecurityAuthorization").getInterfaceFourni("SecurityAuthorization"), 
				(RoleFrom) clearanceRequest.getRole("Grantor"));
		
		addAttachmentFrom((AttachmentFrom) securityManagerToClearanceRequest,
				securityManager.getInterface("InterfaceSecurityAuthorization").getInterfaceFourni("SecurityAuthorization"), 
				(RoleFrom) clearanceRequest.getRole("Grantor"));
		
		Attachment securityManagerToSecurityQuery = new AttachmentTo("SecurityManagerToSecurityQuery", 
				securityManager.getInterface("InterfaceCredentialQuery").getInterfaceRequis("CredentialQuery"), 
				(RoleTo) securityQuery.getRole("SecurityManager"));
		
		addAttachmentTo((AttachmentTo) securityManagerToSecurityQuery,
				securityManager.getInterface("InterfaceCredentialQuery").getInterfaceRequis("CredentialQuery"), 
				(RoleTo) securityQuery.getRole("SecurityManager"));
		
		Attachment databaseToSecurityQuery = new AttachmentFrom("DatabaseToSecurityQuery", 
				database.getInterface("InterfaceSecurityManagement").getInterfaceFourni("SecurityManagement"), 
				(RoleFrom) securityQuery.getRole("Requestor"));
		
		addAttachmentFrom((AttachmentFrom) databaseToSecurityQuery,
				database.getInterface("InterfaceSecurityManagement").getInterfaceFourni("SecurityManagement"), 
				(RoleFrom) securityQuery.getRole("Requestor"));
		
		Attachment databaseToSqlQuery = new AttachmentFrom("DatabaseToSqlQuery", 
				database.getInterface("InterfaceQuery").getInterfaceFourni("Query"), 
				(RoleFrom) sqlQuery.getRole("Callee"));
		
		addAttachmentFrom((AttachmentFrom) databaseToSqlQuery,
				database.getInterface("InterfaceQuery").getInterfaceFourni("Query"), 
				(RoleFrom) sqlQuery.getRole("Callee"));
		
		Binding serverToConnectionManager = new Binding(receiveRequest, 
				(ComponentInterfacePortFourni) connectionManager.getInterface("InterfaceExternalSocket").getInterfaceFourni("ExternalSocket"));
		
		addBinding(serverToConnectionManager,receiveRequest,
				(ComponentInterfacePortFourni) connectionManager.getInterface("InterfaceExternalSocket").getInterfaceFourni("ExternalSocket"));
	}

}
